unit CProperties;

interface

uses
  Database, Vars, AM,
  Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs,
  StdCtrls, Buttons, DB, Grids, Mask, ExtCtrls, Menus, ComCtrls, ColorGrd,
  DBCtrls;

type
  TFormCProperties = class(TForm)
    BtnCancel: TBitBtn;
    BtnSave: TBitBtn;
    PanelLabel: TPanel;
    LabelLabel1: TLabel;
    PanelButton: TPanel;
    LabelButton1: TLabel;
    EditCommand: TEdit;
    LabelButton2: TLabel;
    RBPlain: TRadioButton;
    RBVariable: TRadioButton;
    LabelVariable: TLabel;
    LabelText: TLabel;
    EditText: TEdit;
    LabelButton3: TLabel;
    EditButtonLabel: TEdit;
    BtnVariable: TBitBtn;
    BtnAM: TBitBtn;
    MainMenu1: TMainMenu;
    Invisible1: TMenuItem;
    CtrlA1: TMenuItem;
    EditVariable: TDBComboBox;
    procedure BtnCancelClick(Sender: TObject);
    procedure RBPlainClick(Sender: TObject);
    procedure FormShow(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure BtnVariableClick(Sender: TObject);
    procedure BtnAMClick(Sender: TObject);
  private
    { Private declarations }
    procedure PopulateVariables;
  public
    { Public declarations }
    kind, lab, val: string;
    procedure IsButton;
  end;

var
  FormCProperties: TFormCProperties;

implementation
uses
   DBClient;

{$R *.DFM}

procedure TFormCProperties.FormCreate(Sender: TObject);
begin
  kind:='label';
  lab:=''; val:='';
end;

procedure TFormCProperties.FormShow(Sender: TObject);
begin
  PopulateVariables;
  if EditVariable.text <> '' then begin
    RBVariable.checked:=true;
    exit;
  end;
  if EditText.enabled then EditText.setfocus
  else if EditButtonLabel.enabled then EditButtonLabel.setfocus;
end;

procedure TFormCProperties.BtnCancelClick(Sender: TObject);
var variable: string;
begin
  if TComponent(sender).tag = 0 then begin
    kind:='';
    close;
    exit;
  end;
  if RBPlain.checked = true then begin
    if pos('|',EditText.text) > 0 then begin
      messagedlg('The label text cannot contain the | character',mtinformation,[mbok],0);
      EditText.setfocus;
      exit;
    end;
  end
  else if RBVariable.checked = true then begin
    variable:=EditVariable.text;
    if variable = '' then begin
      messagedlg('You must select a variable',mtinformation,[mbok],0);
      EditVariable.setfocus;
      exit;
    end;
    if pos(' ',variable) > 0 then begin
      messagedlg('Variables cannot contain spaces',mtinformation,[mbok],0);
      EditVariable.setfocus;
      exit;
    end;
    if copy(variable,1,1) <> '@' then begin
      messagedlg('The @ symbol must be included with the variable',
        mtinformation,[mbok],0);
      EditVariable.setfocus;
      exit;
    end;
    delete(variable,1,1);
  end;
  if EditButtonLabel.enabled then begin
    if pos('|',EditButtonLabel.text) > 0 then begin
      messagedlg('The button text cannot contain the | character',mtinformation,[mbok],0);
      EditButtonLabel.setfocus;
      exit;
    end;
    if pos('|',EditCommand.text) > 0 then begin
      messagedlg('The button command cannot contain the | character',mtinformation,[mbok],0);
      EditCommand.setfocus;
      exit;
    end;
  end;
  if EditCommand.enabled then begin
    lab:=EditButtonLabel.text;
    val:=EditCommand.text;
  end
  else begin
    if EditText.enabled then lab:=EditText.text
    else val:=EditVariable.text;
  end;
  close;
end;

procedure TFormCProperties.RBPlainClick(Sender: TObject);
begin
  if RBPlain.checked then begin
    EditVariable.enabled:=false;
    EditVariable.color:=clsilver;
    EditVariable.tabstop:=false;
    BtnVariable.enabled:=false;
    LabelVariable.font.color:=clgray;
    EditText.enabled:=true;
    EditText.color:=clwindow;
    EditText.tabstop:=true;
    LabelText.font.color:=clblack;
    EditText.setfocus;
  end
  else begin
    EditVariable.enabled:=true;
    EditVariable.color:=clwindow;
    EditVariable.tabstop:=true;
    BtnVariable.enabled:=true;
    LabelVariable.font.color:=clblack;
    EditText.enabled:=false;
    EditText.color:=clsilver;
    EditText.tabstop:=false;
    LabelText.font.color:=clgray;
    EditVariable.setfocus;
  end;
end;

procedure TFormCProperties.IsButton;
begin
  RBPlain.font.color:=clgray;
  RBPlain.enabled:=false;
  RBVariable.font.color:=clgray;
  RBVariable.enabled:=false;
  LabelLabel1.font.color:=clgray;
  LabelText.font.color:=clgray;
  EditText.enabled:=false;
  EditText.color:=clsilver;
  EditText.tabstop:=false;
  EditCommand.enabled:=true;
  BtnAM.enabled:=true;
  EditCommand.color:=clwindow;
  EditCommand.tabstop:=true;
  LabelButton1.font.color:=clblack;
  LabelButton2.font.color:=clblack;
  LabelButton3.font.color:=clblack;
  EditButtonLabel.enabled:=true;
  EditButtonLabel.color:=clwindow;
  EditButtonLabel.tabstop:=true;
  kind:='button';
end;

procedure TFormCProperties.PopulateVariables;
var
  table: TClientDataset;
  i: integer;
begin
  EditVariable.items.clear;
  table:=TClientDataset.create(self);
  with table do begin
    CloneCursor(Datawell.TableVariables, false);
    filtered := true;
    active:=true;
    first;
    while not EOF do begin
      EditVariable.items.add('@'+fieldbyname('FVariable').asstring);
      next;
    end;
  end;
  table.free;
  for i:=0 to Datawell.rvars.count-1 do
    EditVariable.items.add('@'+Datawell.rvars[i]);
end;

procedure TFormCProperties.BtnVariableClick(Sender: TObject);
var form: TFormVars;
begin
  form:=TFormVars.create(self);
  form.nodelete:=true;
  form.BtnDelete.enabled:=false;
  form.showmodal;
  form.free;
  PopulateVariables;
end;

procedure TFormCProperties.BtnAMClick(Sender: TObject);
var
  form: TFormAM;
  am: string;
begin
  if (TComponent(sender).tag = 999) and (ActiveControl <> EditCommand) then exit;
  form:=TFormAM.create(self);
  form.setup;
  form.showmodal;
  am:=form.am;
  form.free;
  if am = '' then exit;
  EditCommand.text:=am;
  EditCommand.setfocus;
end;

end.
